package com.qbkj.base_lib.ui;

import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import com.qbkj.base_lib.utils.ActivityManagement;
import com.qbkj.base_lib.utils.TagUtils;
import com.zhy.autolayout.AutoLayoutActivity;


/**
 * Created by Administrator on 2017/5/16 0016.
 */

public abstract class FrameBaseActivity extends AutoLayoutActivity implements View.OnClickListener {
    protected Intent mIntent;
    protected Bundle mBundle;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //防止重复创建的问题，第一次安装完成启动，点home键launcher icon启动会重复
        if (!isTaskRoot() && getIntent().hasCategory(Intent.CATEGORY_LAUNCHER) && getIntent().getAction() != null && getIntent().getAction().equals(Intent.ACTION_MAIN)) {
            finish();
            return;
        }
        mIntent = new Intent();
        mBundle = new Bundle();
        onQbkjCreate(savedInstanceState);
        setTranslucentStatus();
        ActivityManagement.getInstance().addActivity(this);
    }

    //判断当前设备版本号是否为4.4以上，如果是，则通过调用setTranslucentStatus让状态栏变透明
    protected void setTranslucentStatus() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            setTranslucentStatus(true);
        }
    }

    protected abstract void onQbkjCreate(Bundle savedInstanceState);


    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityManagement.getInstance().removeActivity(this);
    }

    @Override
    public void onClick(View v) {

    }

    /**
     * 获取当前设备状态栏高度
     *
     * @return
     */
    public int getStatusBarHeight() {
        int result = 0;
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }

    /**
     * 是否显示状态栏
     *
     * @param on
     */
    @TargetApi(19)
    private void setTranslucentStatus(boolean on) {
        Window win = getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        if (on) {
            winParams.flags |= bits;
        } else {
            winParams.flags &= ~bits;
        }
        win.setAttributes(winParams);
    }


    //跳转
    public void skipActivity(Class cls, int ResultCode, Intent intent, Bundle bundle) {
        intent.setClass(this, cls);
        if (bundle != null) intent.putExtras(bundle);
        if (TagUtils.NORESULTCODE != ResultCode) {
            startActivityForResult(intent, ResultCode);
        } else {
            startActivity(intent);
        }
        mBundle = null;
    }

    protected void skipActivity(Class cls, int ResultCode) {
        skipActivity(cls, ResultCode, mIntent, mBundle);
    }

    public void skipActivity(Class cls) {
        skipActivity(cls, TagUtils.NORESULTCODE, mIntent, mBundle);
    }

    protected void skipActivityFinish(Class cls) {
        skipActivity(cls);
        finish();
    }

    protected void skipActivityFinish(Class cls, int ResultCode) {
        skipActivity(cls, ResultCode);
        finish();
    }

    public void finish(int result) {
        setResult(result);
        finish();
    }


    protected void toast(String text) {
        if (text != null) {
            Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
        }
    }

}



